package com.baichuan.zntx.common;

import smile.math.rbf.GaussianRadialBasis;

public class RBFModel {
    private final double[][] points;
    private final double[] values;
    private final GaussianRadialBasis rbf;

    public RBFModel(double[][] points, double[] values, double sigma) {
        this.points = points;
        this.values = values;
        // 参数可调，表示 RBF 的扩展度
        this.rbf = new GaussianRadialBasis(sigma);
    }

    public double interpolate(double[] point) {
        double result = 0.0;
        double sumWeights = 0.0;

        // 计算加权插值
        for (int i = 0; i < points.length; i++) {
            double distance = distance(point, points[i]);
            double weight = rbf.apply(distance);
            result += weight * values[i];
            sumWeights += weight;
        }

        // 归一化
        return result / sumWeights;
    }

    // 计算两个点之间的欧几里得距离
    private double distance(double[] p1, double[] p2) {
        double sum = 0.0;
        for (int i = 0; i < p1.length; i++) {
            double diff = p1[i] - p2[i];
            sum += diff * diff;
        }
        return Math.sqrt(sum);
    }
}
